/**
 * author: zhou
 * date: 2017.10.23
 * description: 班级管理中心
 * functions: {
 *      save: 保存班级信息
 *      findAll: 获取全部班级
 *      findOne: 获取班级信息
 *      update: 更新班级
 * }
 * 
 */

const   mongoPromise = require('../../../core/mongo/mongoPromise'),
        Class        = require('../../../core/mongo/models/school/classModel');

const classController = {
    /**
     * 保存班级信息
     * @param {*} ctx 
     */
    async save(ctx) {
        const { name, schoolId, gradeId, subjectIds, count } = ctx.request.body.data;
        if (!name) {
            ctx.body = { success: false, message: '班级名称不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        if (!gradeId) {
            ctx.body = { success: false, message: '年级id不能为空', state: 10004 };
            return;
        }
        if (!subjectIds || !Array.isArray(subjectIds) || subjectIds.length === 0) {
            ctx.body = { success: false, message: '科目id不能为空', state: 10004 };
            return;
        }
        const newClass = new Class({
            name,
            schoolId,
            gradeId,
            subjectIds,
            studentsCount: count || 0
        });
        await Class.create(newClass).then((doc) => {
            ctx.body = { success: true, data: [], state: 200 };
        },
        (error) => {
            ctx.body = { success: false, message: '抱歉,保存班级失败', state: 10003 };
        });
    },
    /**
    * 获取班级列表
    * @param {*} ctx 
    */
    async findAll(ctx) {
        const { schoolId, gradeId, date, limit } = ctx.request.body.data;
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        const queryParam = { schoolId };
        // 存在年级id,则添加gradeId查询参数
        if (gradeId) {
            queryParam.gradeId = gradeId;
        }
        if (date) {
            queryParam.createDate = { $gt: date };
        }
        let query = Class.find(queryParam);
        if (limit) {
            query = query.limit(limit);
        }
        query = query.populate('gradeId', { name: 1 }).populate('subjectIds', { _id: 0, name: 1 });
        const result = await mongoPromise(ctx, query);
        if (result) {
            const countQuery = Class.count({ schoolId });
            const countResult = await mongoPromise(ctx, countQuery);
            if (countResult !== undefined) {
                ctx.body = { success: true, data: { data: result, count: countResult }, state: 200 };
            }
        }
    },
    /**
    * 查询班级信息
    * @param {*} ctx 
    */
    async findOne(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '班级id不能为空', state: 10004 };
            return;
        }
        const query = Class.findById(id);
        const result = await mongoPromise(ctx, query);
        if (result !== undefined) {
            ctx.body = { success: true, data: result, state: 200 };
        } else {
            ctx.body = { success: false, message: '抱歉,未查找到该班级', state: 200 };
        }
    },
    /**
     * 更新班级信息
     * @param {*} ctx 
     */
    async update(ctx) {
        const { _id, name, schoolId, gradeId, subjectIds, count } = ctx.request.body.data;
        if (!_id) {
            ctx.body = { success: false, message: '班级id不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '班级名称不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        if (!gradeId) {
            ctx.body = { success: false, message: '年级id不能为空', state: 10004 };
            return;
        }
        if (!subjectIds) {
            ctx.body = { success: false, message: '科目id不能为空', state: 10004 };
            return;
        }
        const newClass = {
            name,
            schoolId,
            gradeId,
            subjectIds,
            studentsCount: count || 0
        };
        const query = Class.update({ _id }, { $set: newClass });
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    }
};

module.exports = classController;
